/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyh.actionitembadge_ohos.other;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import com.mikepenz.actionitembadge.library.utils.Logger;
import com.zyh.actionitembadge_ohos.MainAbility;
import com.zyh.actionitembadge_ohos.ResourceTable;
import com.zyh.actionitembadge_ohos.slice.MainAbilitySlice;

import java.util.List;

/**
 * @since 2021-06-10
 */
public class ListAdapter extends BaseItemProvider {
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;

    private Context mContext;
    private List<ListBeanContent> beanContentList;

    /**
     * 列表构造器
     * @param context
     * @param list
     **/
    public ListAdapter(Context context, List<ListBeanContent> list) {
        mContext = context;
        beanContentList = list;
    }

    @Override
    public int getCount() {
        return beanContentList.size();
    }

    @Override
    public ListBeanContent getItem(int position) {
        return beanContentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemComponentType(int position) {
        if (position == 0) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        HolderView holderView = null;
        Component component1 = component;
        int type = getItemComponentType(position);
        if (component1 == null) {
            if (type == TYPE_2) {
                component1 = LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_normal_item_content, null, false);
                holderView = new HolderView();
                holderView.ohos_title = (Text) component1.findComponentById(ResourceTable.Id_normal_item_title);
                holderView.ohos_author = (Text) component1.findComponentById(ResourceTable.Id_normal_item_author);
                holderView.ohos_content = (Text) component1.findComponentById(ResourceTable.Id_normal_item_txt);
                holderView.ohos_version = (Text) component1.findComponentById(ResourceTable.Id_normal_item_version);
                holderView.ohos_toolVersion = (Text) component1.findComponentById(ResourceTable.Id_normal_item_toolVersion);
                holderView.actionItem_DirItem = (DirectionalLayout) component1.findComponentById(
                    ResourceTable.Id_actionItem_DirItem);
                component1.setTag(holderView);
            } else if (type == TYPE_1) {
                component1 = LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_top_item_content, null, false);
            }
        } else {
            holderView = (HolderView) component1.getTag();
        }
        if (type == TYPE_2) {
            holderView.ohos_title.setText(beanContentList.get(position).getTitle());
            holderView.ohos_author.setText(beanContentList.get(position).getAuthor());
            holderView.ohos_content.setText(beanContentList.get(position).getTxtContent());
            holderView.ohos_version.setText(beanContentList.get(position).getVersion());
            holderView.ohos_toolVersion.setText(beanContentList.get(position).getApaVersion());
            String url = beanContentList.get(position).getUrl();
            holderView.ohos_content.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                }
            });
            if (position == 1) {
                holderView.actionItem_DirItem.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                    }
                });
            }

        }
        return component1;
    }

    class HolderView {
        Text ohos_title;
        Text ohos_author;
        Text ohos_content;
        Text ohos_version;
        Text ohos_toolVersion;
        DirectionalLayout actionItem_DirItem;
    }
}
